package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.BizParts;
import com.example.service.BizPartsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/parts")
public class PartsController {
    @Autowired
    private BizPartsService bizPartsService;

    @RequestMapping("/list")
    public String listParts(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, Map map) {
        PageHelper.startPage(pageNum, pageSize);
        List<BizParts> list = bizPartsService.list();
        log.info(list.toString());
        PageInfo pageInfo = new PageInfo<>(list);
        map.put("pageInfo", pageInfo);
        map.put("pageNum", pageNum);
        return "/partssys/parts/partslist";
    }

    @RequestMapping("/edit")
    public String editParts(Integer partsid, Map map) {
        BizParts parts = bizPartsService.getById(partsid);
        map.put("parts", parts);
        log.info(parts.toString());
        return "/partssys/parts/partsedit";
    }

    @RequestMapping("/update")
    public String updatePartsById(BizParts bizParts) {
        log.info(bizParts.toString());
        bizPartsService.updateById(bizParts);
        return "redirect:/parts/list";
    }

    @RequestMapping("/add")
    public String addParts(BizParts bizParts) {
        log.info(bizParts.toString());
        bizPartsService.save(bizParts);
        return "redirect:/parts/list";
    }

    @RequestMapping("/delete")
    public String deleteUser(Integer partsid) {
        bizPartsService.removeById(partsid);
        return "redirect:/parts/list";
    }

    @RequestMapping("/selectParts.do")
    public String selectParts(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, String partsname, Map map) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<BizParts> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("partsname", partsname);
        List<BizParts> list = bizPartsService.list(queryWrapper);
        log.info(list.toString());
        PageInfo pageInfo = new PageInfo<>(list);
        map.put("pageInfo", pageInfo);
        map.put("pageNum", pageNum);
        return "/partssys/parts/partslist";
    }
}
